package com.example.feigntest.web;


import com.example.feigntest.domain.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
public class HelloController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;


    @RequestMapping("/hello")
    public String index()
    {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
        return "hello world!";
    }

    @RequestMapping("/user")
    @ResponseBody
    public User user()
    {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/user,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
        return new User("bear","123");
    }

    @RequestMapping("/user/{id}")
    @ResponseBody
    public User getUserById(@PathVariable int id)
    {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/getUserById,host:"+instance.getHost()+",service_id:"+instance.getServiceId()+",id:"+id);
        return new User("bear"+id,"123");
    }

    @RequestMapping("/users")
    @ResponseBody
    public List<User> getUsersByIds(@RequestParam List<Integer> ids)
    {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/getUsersByIds,host:"+instance.getHost()+",service_id:"+instance.getServiceId()+",ids:"+ids);
        int size = ids.size();
        List<User> users = new ArrayList<User>();
        for(int i = 0;i<size;i++)
        {
            Integer id = ids.get(i);
            users.add(new User("bear"+Integer.valueOf(id),"123"));
        }
        return users;
    }




}
